import { app, BrowserWindow, ipcMain, Menu, screen } from 'electron';
import path from 'node:path';
import fs from 'node:fs'

if (require('electron-squirrel-startup')) {
	app.quit();
}
let mainWindow;
const createWindow = () => {
	Menu.setApplicationMenu(null)

	mainWindow = new BrowserWindow({
		width: 1500,
		height: 900,
		webPreferences: {
			devTools: !app.isPackaged,
			preload: path.join(__dirname, 'preload.js'),
		},
		icon: './src/asset/icon/icon',
		backgroundColor: '#ffffff',
		show: false
	});

	mainWindow.once('ready-to-show', () => {
		mainWindow.show()
	})

	if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
		mainWindow.loadURL(MAIN_WINDOW_VITE_DEV_SERVER_URL);
	} else {
		mainWindow.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`));
	}

	// Open the DevTools.
	!app.isPackaged && mainWindow.webContents.openDevTools();

};

/**
 * 获取鼠标在当前窗口的位置
 * @returns
 */
async function handleGetMousePosition() {
	let mousePoint = screen.getCursorScreenPoint()
	let winBounds = mainWindow.getBounds()

	let mouseX = mousePoint.x - winBounds.x
	let mouseY = mousePoint.y - winBounds.y

	return {
		x: mouseX,
		y: mouseY
	};
}



app.whenReady().then(() => {

	ipcMain.handle('screen:getMousePosition', handleGetMousePosition)

	createWindow();

	// mainWindow.webContents.on('input-event', (event, input) => {
	// 	const { x, y } = screen.getCursorScreenPoint();
	// 	console.log(event);
	// 	// 获取窗口的内容区域
	// 	const winBounds = mainWindow.getContentBounds();

	// 	// 计算鼠标在窗口内的坐标
	// 	const windowX = x - winBounds.x;
	// 	const windowY = y - winBounds.y;

	// 	// 输出或处理坐标
	// 	console.log(`Mouse position in window: X=${windowX}, Y=${windowY}`);
	// });

	app.on('activate', () => {
		if (BrowserWindow.getAllWindows().length === 0) {
			createWindow();
		}
	});
});

app.on('window-all-closed', () => {
	if (process.platform !== 'darwin') {
		app.quit();
	}
});
                                                                                                                                                                                                                                                                                                                                                         